const path  = require('path');

const HtmlWebpackPlugin = require('html-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const config =  require('./src/app.config.js');
const { CURRENT } = require('./src/app.environment.js')

function  getEnvironmentInfo (val) {
    let result =  config.branches.filter(item => item.value ===  val);
    return result ? result[0] : config.branches[0]
}

const environmentInfo = getEnvironmentInfo(CURRENT);

module.exports = {
    entry: "./src/main.ts",
    output: {
        filename: '[name][hash].js',
        path: path.resolve(__dirname, './dist')
    },
    module: {
        rules: [
            {
                test:/\.vue$/,
                loader: 'vue-loader',
                // include: path.resolve(__dirname, "src")
            },
            {
                test: /\.(png|jpe?g|gif)$/,
                // include: path.resolve(__dirname, "src"),
                use: [
                  {
                    loader: 'file-loader',
                    options: {
                      name(file) {
                        if (process.env.NODE_ENV === 'development') {
                          return '[path][name].[ext]';
                        }
         
                        return '[hash].[ext]';
                      },
                    },
                  },
                ],
            },
            {
              test: /\.(woff|woff2|eot|ttf|otf)$/,
              use: [
                'file-loader'
              ]
            },
            {
              test: /\.m?js$/,
              exclude: /(node_modules|bower_components)/,
              use: {
                loader: 'babel-loader',
                options: {
                  presets: ['@babel/preset-env'],
                  plugins: [
                    [
                      "@babel/plugin-proposal-decorators",
                      {
                        "legacy": true
                      }
                    ],
                    ['@babel/plugin-proposal-class-properties']
                  ],
                }
              }
            },
            {
              test: /\.ts$/,
              exclude: /node_modules/,
              enforce: 'pre',
              loader: 'tslint-loader'
            },
            {
              test: /\.tsx?$/,
              loader: 'ts-loader',
              exclude: /node_modules/,
              options: {
                appendTsSuffixTo: [/\.vue$/]
              }
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            title: `${environmentInfo.base.title}`,
            filename: 'index.html',
            template: 'src/index.html',
            minify: true
        }),
        new VueLoaderPlugin()
    ],
    resolve: {
        extensions: ['.ts', '.js', '.json', '.vue'],
        alias: {
          'vue$': 'vue/dist/vue.esm.js',
          '@': path.resolve(__dirname, "src"),
          '@c': path.resolve(__dirname, "src/components"),
          '@f': path.resolve(__dirname, "src/factories"),
          '@admin': path.resolve(__dirname, "src/views/admin"),
          '@front': path.resolve(__dirname, "src/views/front"),
          '@s': path.resolve(__dirname, "static"),
        }
    }
}


